package com.zj.leetcode.medium;

/**
 * @program algorithm
 * @description:
 * @author: zhangbo
 * @create: 2022/01/06 19:20
 */
public class ReverseWordsInAString {
    public static void main(String[] args) {
        String s = "  example  ";
        Solution solution = new ReverseWordsInAString().new Solution();
        System.out.println(solution.reverseWords(s));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String reverseWords(String s) {

            char[] c = s.toCharArray();
            StringBuilder newS = new StringBuilder();
            for (int i = 0; i < c.length; i++) {
                if("".equals(newS.toString()) && " ".equals(String.valueOf(c[i]))){
                    continue;
                }
                if (" ".equals(String.valueOf(c[i])) && " ".equals(String.valueOf(c[i - 1]))) {
                    continue;
                }


                newS.append(c[i]);
            }
            String[] split = newS.toString().split(" ");

            int i = 0;
            while (i <= (split.length - 1) / 2) {
                String temp = split[i];
                split[i] = split[split.length - 1 - i];
                split[split.length - 1 - i] = temp;
                i++;
            }
            String ret = "";
            for (int j = 0; j < split.length; j++) {
                if(j == 0){
                    ret = ret + split[j];
                }else {
                    ret = ret + " " + split[j];
                }

            }

            return ret;
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}